//
//  XQEmergencyView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/11.
//

import UIKit

class XQEmergencyView: UIView {
    
    var onTapHandle: ((String) -> Void)?
    
    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return lb
    }()
    
    private lazy var bgView : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#F6F6F6")
        view.layer.cornerRadius = 12
        return view
    }()
    
    private lazy var textField : UITextField = {
        let textField = UITextField()
        textField.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        textField.textColor = .xq_black
        textField.keyboardType = .asciiCapable
        return textField
    }()
    
    private lazy var arrowImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.sizeToFit()
        return imageView
    }()
    
    private lazy var button : UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        return button
    }()
    
    var isPhone : Bool = false {
        didSet {
            self.arrowImageView.image = UIImage(named: isPhone ? "emergency_arrow" : "public_accure_arrow")
            self.arrowImageView.snp.remakeConstraints { make in
                make.centerY.equalToSuperview()
                make.right.equalTo(isPhone ? -14 : -20)
            }
        }
    }
    
    var title : String? = "" {
        didSet {
            self.titleLb.text = title
        }
    }
    
    var placeHolder : String? = "" {
        didSet {
            let attributes: [NSAttributedString.Key: Any] = [
                .font: UIFont.systemFont(ofSize: 15),
                .foregroundColor: UIColor(hex: "#A3A6A9")!
            ]
            textField.attributedPlaceholder = NSAttributedString(string: placeHolder ?? "", attributes: attributes)
        }
    }
    
    var content : String? = "" {
        didSet {
            self.textField.text = content
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        makeUI()
        makeConstraints()        
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        self.addSubview(self.titleLb)
        self.addSubview(self.bgView)
        self.bgView.addSubview(self.textField)
        self.bgView.addSubview(self.arrowImageView)
        self.bgView.addSubview(self.button)
    }
    
    private func makeConstraints() {
        self.titleLb.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(17)
        }
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLb.snp.bottom).offset(16)
            make.left.equalTo(17)
            make.right.equalTo(-17)
            make.height.equalTo(50)
            make.bottom.equalTo(0)
        }
        self.textField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.top.bottom.equalTo(0)
            make.right.equalTo(-50)
            make.left.equalTo(16)
        }
        self.arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-20)
        }
        self.button.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    @objc private func buttonClick() {
        onTapHandle?(textField.text ?? "")
    }
}
